/*package com.utils.spring;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.springframework.core.annotation.Order;

import com.google.common.collect.Maps;
import com.kyhd.toutiao.util.RequestContextLog;
import com.kyhd.toutiao.util.ThreadLocalUtil;

import lombok.extern.slf4j.Slf4j;

@Order(1)
@Slf4j
@WebFilter(filterName = "logFilter1", urlPatterns = {
        "/tt/api/*"
        })
public class LogFilter implements Filter {
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
//		HttpServletResponse response = (HttpServletResponse) servletResponse;

		RequestContextLog reqContext = new RequestContextLog();
		String uri = request.getRequestURI();
		Enumeration<?> pNames = request.getParameterNames();
		Map<String, String> params = Maps.newHashMap();
	    StringBuilder parameters = new StringBuilder();
		while (pNames.hasMoreElements()) {
			String pName = (String) pNames.nextElement();
			String pValue = request.getParameter(pName);
	        parameters.append(pName + ":" + pValue + " || ");
			params.put(pName, pValue);
			if("deviceID".equalsIgnoreCase(pName)) {
				reqContext.setDeviceId(pValue);
			}
		}
		
		Enumeration<?> hNames = request.getHeaderNames();
	    StringBuilder headers = new StringBuilder();
		while (hNames.hasMoreElements()) {
			String hName = (String) hNames.nextElement();
			String hValue = request.getHeader(hName);
			headers.append(hName + ":" + hValue + " || ");
			params.put(hName, hValue);
			if("deviceID".equalsIgnoreCase(hName)) {
				reqContext.setDeviceId(hValue);
			}
			if("User-Agent".equalsIgnoreCase(hName)) {
				reqContext.setUa(hValue);
			}
			
			try {
				if("X-Forwarded-For".equalsIgnoreCase(hName)) {
					reqContext.setIp(hValue.split(",")[0]);
				}
			} catch (Exception e) {
				if("X-Real-IP".equalsIgnoreCase(hName)) {
					reqContext.setIp(hValue);
				}
			}
			//版本渠道号
			if("ve".equalsIgnoreCase(hName)) {
				if(hValue.contains("_")) {
					String[] ve = hValue.split("_");
					reqContext.setVe(hValue);
					if(ve.length>1) {
						reqContext.setAppVersion(ve[0]);				
						reqContext.setChannelCode(ve[1]);
					}else {
						reqContext.setAppVersion(ve[0]);				
					}
				}else{
					reqContext.setVe(hValue);
					reqContext.setAppVersion(hValue);				
				}
			}
			
			if("nonce".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setNonce(null);
				else
					reqContext.setNonce(hValue);
			}
			if("accessToken".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setAccessToken(null);
				else
					reqContext.setAccessToken(hValue);
			}
			if("timestamp".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setTimestamp(null);
				else
					reqContext.setTimestamp(hValue);
			}
			if("mediaSource".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setMediaSource(null);
				else
					reqContext.setMediaSource(hValue);
			}
			if("gender".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setGender(null);
				else
					reqContext.setGender(hValue);
			}
			
			if("isad".equalsIgnoreCase(hName)) {
				if("null".equalsIgnoreCase(hValue) || "undefined".equalsIgnoreCase(hValue))
					reqContext.setIsad(null);
				else
					reqContext.setIsad(hValue);
			}
			
		}
		
		log.info("URI:{"+uri+"}, "+"Parameter:{"+parameters.toString()+"}, "+"Headers:{"+headers.toString()+"}");
		
		//获取api版本
		reqContext.setApiVersion(getVersion(uri));
		if("null".equalsIgnoreCase(params.get("uuid")) ||"undefined".equalsIgnoreCase(params.get("uuid")))
			reqContext.setUuid(null);
		else
			reqContext.setUuid(params.get("uuid"));
		ThreadLocalUtil.requestCtxTL.set(reqContext);
		long start = System.currentTimeMillis();
        filterChain.doFilter(servletRequest, servletResponse);
        long end = System.currentTimeMillis();
        log.info("URI:{} completed response time:{}",uri,end-start);
		ThreadLocalUtil.requestCtxTL.remove();
	}
	
	@Override
	public void destroy() {
		
	}

	*//**
	 * 获取uri中的版本号
	 * @param uri
	 * @return
	 *//*
	public static String getVersion(String uri) {
		String apiVersion="FIRST";
		int index = getIndex(uri,"(v|V)(\\d+)");
		if(index!=-1) {
			String str = uri.substring(index,uri.length());
			apiVersion = str.substring(0,str.indexOf("/"));
		}
		return apiVersion;
	}
	
	*//**
	 * 获取匹配的正则字符串的起始索引
	 * @param str 要验证的字符串
	 * @param regex 验证规则
	 * @return
	 *//*
	public static int getIndex(String str,String regex) {
	    Pattern pattern = Pattern.compile(regex);
	    Matcher matcher = pattern.matcher(str);
//	    boolean match = matcher.matches();
	    boolean match = matcher.find();
		int index = -1;
	    if(match) {
	    	index = matcher.start();
	    }
		return index;
	}
	
}
*/